package VIEW;

import CONF.ConexaoOracle;
import MODEL.EspectadorModel;
import MODEL.LineUpModel;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import javax.swing.JOptionPane;

public class ConfirmacaoView extends javax.swing.JFrame {
    
    private ArrayList<LineUpModel> lineUpArray;
    public Object[][] dados;
    private Integer totalSelecionados;

    /**
     * Creates new form ConfirmacaoView
     * @param dados
     */
    public ConfirmacaoView(ArrayList<LineUpModel> lineUpArray, Integer totalSelecionados, Object[][] dados) {
        this.lineUpArray       = lineUpArray;
        this.totalSelecionados = totalSelecionados;
        this.dados             = dados;
        
        initComponents();
        
        // 100 é o valor padrão dos ingressos
        for(Object[] object : dados) {
            textoInformacoes.append("Line UP: " + object[0].toString() + " - CPF: " + object[1].toString() + " - Valor: R$ 100,00\n");
        }
        
        textoInformacoes.append("\n");
        textoInformacoes.append("Total de ingressos: " + dados.length + "\n");
        textoInformacoes.append("Valor por ingresso: 100,00\n");
        textoInformacoes.append("\n");
        textoInformacoes.append("Valor total a pagar: R$ " + dados.length*100 + ",00");
    }
    
    public ConfirmacaoView() {
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        painelConfirmacao = new javax.swing.JPanel();
        labelTitulo = new javax.swing.JLabel();
        botaoVoltar = new javax.swing.JButton();
        botaoConfirmar = new javax.swing.JButton();
        painelInformacoes = new javax.swing.JScrollPane();
        textoInformacoes = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Confirmação de Compra");

        labelTitulo.setText("Confirmação de Compra de Ingressos");

        botaoVoltar.setText("Voltar");
        botaoVoltar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoVoltarActionPerformed(evt);
            }
        });

        botaoConfirmar.setText("Confirmar");
        botaoConfirmar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoConfirmarActionPerformed(evt);
            }
        });

        textoInformacoes.setColumns(20);
        textoInformacoes.setRows(5);
        textoInformacoes.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        textoInformacoes.setEnabled(false);
        painelInformacoes.setViewportView(textoInformacoes);

        javax.swing.GroupLayout painelConfirmacaoLayout = new javax.swing.GroupLayout(painelConfirmacao);
        painelConfirmacao.setLayout(painelConfirmacaoLayout);
        painelConfirmacaoLayout.setHorizontalGroup(
            painelConfirmacaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(painelConfirmacaoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(painelConfirmacaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(painelInformacoes)
                    .addGroup(painelConfirmacaoLayout.createSequentialGroup()
                        .addComponent(botaoVoltar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(botaoConfirmar))
                    .addGroup(painelConfirmacaoLayout.createSequentialGroup()
                        .addComponent(labelTitulo)
                        .addGap(0, 112, Short.MAX_VALUE)))
                .addContainerGap())
        );
        painelConfirmacaoLayout.setVerticalGroup(
            painelConfirmacaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(painelConfirmacaoLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelTitulo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(painelInformacoes, javax.swing.GroupLayout.DEFAULT_SIZE, 218, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(painelConfirmacaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botaoVoltar)
                    .addComponent(botaoConfirmar))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(painelConfirmacao, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(painelConfirmacao, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        painelConfirmacao.getAccessibleContext().setAccessibleName("painelConfirmacao");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botaoConfirmarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoConfirmarActionPerformed
        this.dispose();
        
        // atribuindo ingressos
        for(Object[] object : dados) {
            
            EspectadorModel espectador = new EspectadorModel();
            espectador.setCpf((Integer) object[1]);
            
            try {
                espectador.atribuirIngressoDB(ConexaoOracle.conexao, espectador.dataStringToCalendar(object[0].toString()), 100.00f);
                //System.out.println(object[0].toString());
                //System.out.println(object[1].toString());
            } catch (ParseException | SQLException e) {
                JOptionPane.showMessageDialog(null, "Erro ao atribuir ingresso do dia " + object[0].toString() + " para " + object[1].toString());
            }
        }
        
        EscolhaView escolhaView = new EscolhaView();
        escolhaView.setLocationRelativeTo(null);
        escolhaView.setVisible(true);
    }//GEN-LAST:event_botaoConfirmarActionPerformed

    private void botaoVoltarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoVoltarActionPerformed
        this.dispose();
        AssociarIngressosView associarIngressosView = new AssociarIngressosView(lineUpArray, totalSelecionados);
        associarIngressosView.setLocationRelativeTo(null);
        associarIngressosView.setVisible(true);
    }//GEN-LAST:event_botaoVoltarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ConfirmacaoView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ConfirmacaoView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ConfirmacaoView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ConfirmacaoView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ConfirmacaoView().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botaoConfirmar;
    private javax.swing.JButton botaoVoltar;
    private javax.swing.JLabel labelTitulo;
    private javax.swing.JPanel painelConfirmacao;
    private javax.swing.JScrollPane painelInformacoes;
    private javax.swing.JTextArea textoInformacoes;
    // End of variables declaration//GEN-END:variables
}
